<template>
  <div class="login-wrap">
    <div class="bg">
      <img src="@/static/bg-daa6ee4f.svg" alt="My Happy SVG" />
    </div>
    <div class="wrapper">
      <div class="login">
        <div class="auth">
          <div class="desc">
            <h1 class="welcome">欢迎回来！</h1>
            <div>
              <p style="line-height: 28px" v-if="!content">很高兴再次见到您!</p>
              <div v-else style="font-size: 14px">
                <p v-html="content"></p>
              </div>
            </div>
          </div>
          <div class="form">
            <p
              style="
                font-size: 12px;
                text-align: left;
                color: #c2c2c2;
                margin-bottom: 6px;
              "
            >
              请输入授权登陆码<span style="color: red">*</span>
            </p>
            <input
              v-model="code"
              class="code-input"
              placeholder="请输入授权码"
            />
            <u-button
              type="primary"
              class="login-btn"
              :loading="loading"
              @click="submit"
            >
              {{ loginLoading ? '登录中...' : '登录' }}
            </u-button>
            <!-- <p style="font-size: 12px; color: #c2c2c2; text-align: left; margin-top: 8px">
              不会使用Midjourney?
              <a
                style="color: #409eff; cursor: pointer"
               http://45.153.129.99:36283/database href="https://fia4ernqvtt.feishu.cn/docx/Ke9Cdk0iuoOUL9x97l0cxpmtnbb"
                target="_blank"
                >查看使用教程
              </a>
            </p> -->
          </div>
        </div>
        <div class="qrCode">
          <div class="code-div"><img src="@/static/wechat.jpg" /></div>
          <h1 class="add-wechat">加客服微信</h1>
          <p>反馈建议，合作请加微信</p>
          <p>使用问题请直接联系店铺客服</p>
          <p>(8:30-24:00)</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
  import { onLoad } from '@dcloudio/uni-app'
  import Cookies from 'js-cookie'
  import { getConfig, website, getSystemInfoApi } from '@/api/base'
  import dayjs from 'dayjs'
  defineOptions({
    name: 'IndexPage',
  })
  Cookies.remove() // 清除所有
  const code = ref('')
  const loading = ref(false)
  const content = ref('<span>123</span>')
  const loginLoading = ref(false)
  const __mj_code__ = '__mj_code__'
  const __expires_time__ = '__expires_time__'
  // const __experies__ = 1
  const getHostname = () => {
    const url = new URL(window.location.origin)
    return '.' + url.hostname.split('.').slice(-2).join('.')
  }

  const submit = async () => {
    if (!unref(code)) {
      uni.showModal({
        title: '提示',
        content: '授权码不能为空',
        showCancel: false,
        success: function () {},
      })
      return
    }
    loginLoading.value = true
    try {
      const res = await getConfig(unref(code))
      console.log(res)
      const { success, data = {} } = res.data
      if (!success) {
        uni.$u.toast('账号不存在')
        loginLoading.value = false
        return
      }

      // 判断下是否到期
      if (dayjs().isAfter(dayjs(data.expireTime))) {
        uni.showModal({
          title: '提示',
          content: '授权码已过期，请立即前往续费',
          showCancel: false,
          success: function () {},
        })
        loginLoading.value = false
        return
      }

      Cookies.set(__mj_code__, unref(code), {
        domain: getHostname(),
        path: '/',
        expires: 1, // 一天
      })
      Cookies.set(__expires_time__, data.expireTime, {
        domain: getHostname(),
        path: '/',
        expires: 1, // 一天
      })
      gotoUrl(data)
    } catch (error) {
      //
      uni.$u.toast('账号不存在')
    }
    loginLoading.value = false
  }

  onLoad(() => {
    // if (Cookies.get(__mj_code__)) {
    //   gotoUrl()
    // }
  })

  const init = () => {
    loading.value = true
    getSystemInfoApi()
      .then((res) => {
        const { success, data = {} } = res.data
        if (success) {
          content.value = data.content
        }
      })
      .finally(() => {
        loading.value = false
      })
  }

  const gotoUrl = (p) => {
    let url = '/app'
    if (p.guildId && p.channelId) {
      url = `/channels/${p.guildId}/${p.channelId}`
    }
    window.location.href = website + url
  }

  init()
</script>

<style lang="scss" scoped>
  body {
    font-family: Arial, sans-serif;
    background-color: #f5f5f5;
    overflow: hidden;
  }

  h1 {
    text-align: center;
    margin-top: 50px;
  }

  h3 {
    text-align: center;
  }

  p {
    text-align: center;
  }

  form {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-top: 50px;
  }

  input[type='text'],
  input[type='password'] {
    padding: 10px;
    margin-top: 20px;
    border-radius: 5px;
    border: none;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
    width: 300px;
  }

  button {
    padding: 10px 20px;
    margin-top: 20px;
    border-radius: 5px;
    border: none;
    background-color: #4285f4;
    color: #fff;
    font-size: 16px;
    cursor: pointer;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
  }

  button:hover {
    background-color: #357ae8;
  }
  body,
  h1,
  h2,
  h3,
  h4,
  p,
  ul,
  li {
    padding: 0;
    margin: 0;
  }

  ul,
  li {
    list-style: none;
  }

  a {
    text-decoration: none;
    color: #333;
  }

  img {
    vertical-align: top;
  }

  input {
    width: 100%;
    outline-style: none;
    border-radius: 3px;
    font-size: 14px;
    padding: 10px;
    height: 40px;
    background: #1e1f22;
    box-sizing: border-box;
    color: #dcdee1;
  }

  button {
    border-radius: 5px;
    border: none;
    background-color: #5865f2;
    color: #fff;
    font-size: 16px;
    cursor: pointer;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
  }

  button:hover {
    background-color: #4752c4;
  }

  body {
    background-color: #5865f2;
  }

  .login-wrap {
    position: relative;
    width: 100vw;
    height: 100vh;
    min-height: 408px;
    overflow: auto;
  }

  .bg {
    position: absolute;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background: #5865f2;
  }
  .bg img {
    width: 100%;
  }

  .wrapper {
    background: #313339;
    position: absolute;
    top: 50%;
    left: 50%;
    width: 784px;
    // height: 408px;
    box-shadow: 0 2px 10px 0;
    transform: translate(-50%, -50%);

    &-foot {
      padding-left: 32px;
      padding-top: 20px;
      padding-bottom: 20px;
      display: flex;
      align-items: center;
      background-color: rgba(0, 0, 0, 0.2);
      font-size: 13px;
      color: rgba(#ffffff, 0.4);
    }
  }

  .login {
    display: flex;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: space-between;

    padding: 32px;
    font-size: 18px;
    border-radius: 5px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }
  .login .auth {
    width: 370px;
    height: 100%;
    display: flex;
    flex-direction: column;
    // justify-content: center;
  }
  .login .auth .desc {
    width: 100%;
    text-align: center;
  }
  .login .auth .desc p {
    color: #b5bac1;
    font-size: 16px;
    line-height: 20px;
  }
  .login .auth .welcome {
    color: #f2f3f5;
    margin-bottom: 8px;
    font-size: 24px;
    line-height: 30px;
  }
  .login .auth .form {
    margin-top: 20px;
  }
  .login .auth .form .code-input {
    width: 100%;
    border: none;
  }
  .login .auth .form .login-btn {
    width: 100%;
    background: #5865f2;
    color: #fff;
    box-sizing: border-box;
    height: 42px;
    margin-top: 20px;
  }
  .login .auth .form .login-btn:hover {
    background-color: #4752c4;
  }
  .login .qrCode {
    overflow: hidden;
    width: 240px;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  .login .qrCode .code-div {
    width: 176px;
    height: 176px;
    position: relative;
    margin-bottom: 32px;
    background: #fff;
    border-radius: 5px;
    overflow: hidden;
    margin: 30px 0px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .login .qrCode .code-div img {
    width: 164px;
    height: 164px;
  }
  .login .qrCode .add-wechat {
    color: #f2f3f5;
    margin-bottom: 8px;
    font-size: 24px;
    line-height: 30px;
  }
  .login .qrCode p {
    color: #b5bac1;
    font-size: 16px;
    line-height: 20px;
  }

  .notice {
    width: 784px;
    padding: 12px;
    background: #fff;
    border-radius: 5px;
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translate(-50%, 0%);
    box-shadow: 0px 0px 5px 0 #5865f2;
    box-sizing: border-box;
  }

  .sign {
    display: flex;
    align-items: flex-end;
    margin-right: 10px;

    li {
      width: 3px;
      background-color: #67c23a;
      &:not(:last-child) {
        margin-right: 1px;
      }
    }

    li:nth-child(1) {
      height: 4px;
    }
    li:nth-child(2) {
      height: 8px;
    }

    li:nth-child(3) {
      height: 12px;
    }
  }

  .sign-error {
    li {
      background-color: red;
    }
  }
</style>
